package com.safewind.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.safewind.model.Address;
import com.safewind.model.AddressData;
import com.safewind.model.User;
import com.safewind.service.IAddressGroupService;
import com.safewind.service.IAddressService;
import com.safewind.service.IHistoryAddressService;
import com.safewind.util.StringUtil;

@Controller
@RequestMapping("/address")
public class AddressController {
	@Resource
	private IAddressService addressService;

	@Resource
	private IHistoryAddressService historyAddressService;

	@Resource
	private IAddressGroupService addressGroupService;

	@RequestMapping("/resolver")
	@ResponseBody
	public Object resolver(HttpServletRequest request, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		File file = (File) session.getAttribute("file");
		User user = (User) session.getAttribute("user");
		try {
			List<AddressData> addrList = addressService.saveAddressByExport(
					file, user.getPkUserId());
			historyAddressService.importAddress(user.getPkUserId(),
					addrList.size());
			map.put("addrList", addrList);
			map.put("success", true);
		} catch (IOException e) {
			map.put("msg", "文件上传异常");
		} catch (NumberFormatException e) {
			map.put("msg", "电话号存在错误");
		}
		return map;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Object add(HttpServletRequest request, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		String name = request.getParameter("addressName");
		String phoneNumber = request.getParameter("phoneNumber");
		String groupId = request.getParameter("groupId");
		User user = (User) session.getAttribute("user");
		Address address = new Address();
		address.setPkId(StringUtil.getUUID());
		address.setName(name);
		address.setGroupId(groupId);
		address.setPhoneNumber(phoneNumber);
		address.setAddTime(new Date());
		address.setRemark(null);
		address.setUpdateTime(new Date());
		Address add = addressService.selectAddressByPhoneNumberAndGroup(
				phoneNumber, groupId);
		if (add != null && StringUtil.isNotBlank(add.getPkId())) {
			map.put("success", false);
			map.put("msg", "该号码已存在于分组");
		} else {
			addressService.addAddress(address);
			historyAddressService.addAddress(user.getPkUserId(), name,
					phoneNumber);
			map.put("success", true);
		}
		return map;
	}

	/**
	 * 暂时界面无修改功能
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(HttpServletRequest request, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		String name = request.getParameter("name");
		String phoneNumber = request.getParameter("phone_number");
		String groupId = request.getParameter("group_id");
		String remark = request.getParameter("remark");
		String id = request.getParameter("id");
		Address address = new Address();
		address.setPkId(id);
		address.setName(name);
		address.setGroupId(groupId);
		address.setPhoneNumber(phoneNumber);
		address.setRemark(remark);
		address.setUpdateTime(new Date());
		addressService.updateAddress(address);
		map.put("success", true);
		return map;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(HttpServletRequest request, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		String addressIds = request.getParameter("addressIds");
		User user = (User) session.getAttribute("user");
		addressService.deleteAddressByIds(addressIds);
		historyAddressService.deleteAddress(user.getPkUserId(), 0,
				addressIds.split(",").length);
		map.put("success", true);
		return map;
	}

	@RequestMapping("/move")
	@ResponseBody
	public Object move(HttpServletRequest request, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("addressIds");
		String groupId = request.getParameter("groupId");
		User user = (User) session.getAttribute("user");
		addressService.updateGroupIds(ids, groupId);
		historyAddressService
				.moveAddress(user.getPkUserId(), ids.split(",").length,
						addressGroupService.getAddressGroupById(groupId)
								.getGroupName());
		map.put("success", true);
		return map;
	}
}
